package io.renren.modules.dishes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.renren.common.utils.MinioUtilS;
import io.renren.modules.dishes.dao.RecipesDao;
import io.renren.modules.dishes.entity.RecipeStep;
import io.renren.modules.dishes.entity.RecipesDetail;
import io.renren.modules.dishes.entity.RecipesEntity;
import io.renren.modules.dishes.entity.RecipesVo;
import io.renren.modules.dishes.service.RecipesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class RecipesServiceImpl extends ServiceImpl<RecipesDao, RecipesEntity> implements RecipesService {

    @Autowired
    private RecipesDao recipesDao;

    @Autowired
    private MinioUtilS minioUtilS;
    @Value("${minio.endpoint}")
    private String address;
    @Value("${minio.bucketName}")
    private String bucketName;
    @Override
    public List<RecipesVo> getListBySpuId(String spuId) {
        QueryWrapper<RecipesEntity> queryWrapper=new QueryWrapper<RecipesEntity>()
                .eq("spu_id",spuId);
        List<RecipesEntity> list=this.list(queryWrapper);
        List<RecipesVo> recipesVoList=new ArrayList<>();
        for (int i=0;i<list.size();i++){
            RecipesEntity recipesEntity = list.get(i);
            RecipesVo recipesVo = new RecipesVo();
            recipesVo.setRecipesId(recipesEntity.getRecipesId());
            recipesVo.setSpuId(recipesEntity.getSpuId());

            String recipesDetail = recipesEntity.getRecipesDetail();
            RecipesDetail recipesDetail1 = this.parseRecipesDetail(recipesDetail);
            recipesVo.setRecipesDetailList(recipesDetail1);
            recipesVoList.add(recipesVo);
        }
        return recipesVoList;
    }

    @Override
    public Boolean saveByVO(RecipesVo recipesVo) {
        RecipesEntity recipesEntity = new RecipesEntity();
        String uuid = UUID.randomUUID().toString().replace("-", ""); // 移除短横线，得到32位
        String shortId = uuid.substring(0, 32); // 截取前32位
        RecipesDetail recipesDetail = recipesVo.getRecipesDetailList();
        String jsonString = recipesDetail.toJsonString();
        recipesEntity.setRecipesDetail(jsonString);
        recipesEntity.setRecipesId(shortId);
        recipesEntity.setSpuId(recipesVo.getSpuId());
        return recipesDao.saveRecipes(recipesEntity);
    }


    public RecipesDetail parseRecipesDetail(String jsonString) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            // 将JSON字符串转换为RecipesDetail对象
            RecipesDetail recipesDetail = objectMapper.readValue(jsonString, RecipesDetail.class);
            return recipesDetail;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    @Override
    public RecipesVo getByRecipesId(String recipesId){
        RecipesVo res=new RecipesVo();
        RecipesEntity entity = this.getById(recipesId);
        res.setSpuId(entity.getSpuId());
        res.setRecipesId(recipesId);
        String recipesDetail = entity.getRecipesDetail();
        RecipesDetail recipesDetail1 = this.parseRecipesDetail(recipesDetail);
        res.setRecipesDetailList(recipesDetail1);
        return res;
    }

    @Override
    public Boolean updateByVO(RecipesVo recipesVo) {

        RecipesVo old = this.getByRecipesId(recipesVo.getRecipesId());
        RecipesDetail oldDetail = old.getRecipesDetailList();
        RecipesDetail newDetail = recipesVo.getRecipesDetailList();
        if (!oldDetail.getMainSrc().equals(newDetail.getMainSrc())){
            String mainSrc = oldDetail.getMainSrc();
            String filename = mainSrc.substring(mainSrc.lastIndexOf('/') + 1);
            minioUtilS.removeFile(bucketName, filename);
        }
        List<RecipeStep> oldRecipesDetailList = oldDetail.getRecipesDetailList();
        List<RecipeStep> newDetailRecipesDetailList = newDetail.getRecipesDetailList();
        for (int i=0;i<oldRecipesDetailList.size();i++){
            Boolean flag=true;
            for (int j=0;j<newDetailRecipesDetailList.size();j++){
                if (oldRecipesDetailList.get(i).getSrc().equals(newDetailRecipesDetailList.get(j).getSrc())){
                    flag=false;
                    break;
                }
            }
            if (flag){
                String src=oldRecipesDetailList.get(i).getSrc();
                String filename = src.substring(src.lastIndexOf('/') + 1);
                minioUtilS.removeFile(bucketName, filename);
            }
        }
        RecipesEntity recipesEntity = new RecipesEntity();
        recipesEntity.setRecipesId(recipesVo.getRecipesId());
        recipesEntity.setSpuId(recipesVo.getSpuId());
        String jsonString = recipesVo.getRecipesDetailList().toJsonString();
        recipesEntity.setRecipesDetail(jsonString);
        return this.updateById(recipesEntity);
    }

    @Override
    public Boolean deleteById(String recipesId) {
        RecipesVo byRecipesId = this.getByRecipesId(recipesId);
        RecipesDetail recipesDetailList = byRecipesId.getRecipesDetailList();
        if (recipesDetailList!=null){
            String mainSrc = recipesDetailList.getMainSrc();
            String filename = mainSrc.substring(mainSrc.lastIndexOf('/') + 1);
            minioUtilS.removeFile(bucketName, filename);
            List<RecipeStep> recipesDetailList1 = recipesDetailList.getRecipesDetailList();
            for (int i=0;i<recipesDetailList1.size();i++){
                String src = recipesDetailList1.get(i).getSrc();
                String srcFilename = src.substring(src.lastIndexOf('/') + 1);
                minioUtilS.removeFile(bucketName, srcFilename);
            }
        }
        return this.removeById(recipesId);
    }
}
